<template>
  <div class="wrapper">
    <BackButton2 />
    <!-- header部分 -->
    <header>
      <p>商家管理菜品</p>
    </header>

    <!-- 商家logo部分 -->
    <div class="business-logo">
      <img :src="business.businessImg" />
    </div>

    <!-- 商家信息部分 -->
    <div class="business-info">
      <h1>{{ business.businessName }}</h1>
      <p>&#165;{{ business.starPrice }}起送 &#165;{{ business.deliveryPrice }}配送</p>
      <p>{{ business.businessExplain }}</p>
    </div>

    <!-- 菜品信息列表部分 -->
    <ul class="food">
      <li v-for="(item, index) in foodArr" :key="item.foodId">
        <div class="food-left">
          <!-- 修改价格的小画笔图标 -->
          <i class="fa fa-pencil" @click="openEditPriceDialog(item)" style="font-size: 24px; color: blue; cursor: pointer; margin-right: 10px;"></i>
          <img :src="item.foodImg" />
          <div class="food-left-info">
            <h3>{{ item.foodName }}</h3>
            <p>{{ item.foodExplain }}</p>
            <p>&#165;{{ item.foodPrice }}</p>
          </div>
        </div>
        <div class="food-right">
          <i class="fa fa-trash" @click="deleteFood(item.foodId)" style="font-size: 24px; color: red; cursor: pointer;"></i>
        </div>
      </li>
    </ul>

    <!-- 底部新增菜品按钮 -->
    <div class="add-food" @click="addNewFood">
      <i class="fa fa-plus-circle" style="font-size: 48px; color: green;"></i>
      <p>新增菜品</p>
    </div>

    <!-- 修改价格的弹窗 -->
    <div v-if="showPriceDialog" class="price-dialog">
      <div class="dialog-content">
        <h3>修改价格</h3>
        <input v-model="newPrice" type="number" class="price-input" placeholder="输入新价格" min="0" step="0.01"/>
        <div class="dialog-actions">
          <button @click="updateFoodPrice">确定</button>
          <button @click="closeEditPriceDialog">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BackButton2 from "@/components/BackButton2.vue";

export default {
  name: "MerchantManage",
  components: { BackButton2 },
  data() {
    return {
      businessId: this.$route.query.businessId,
      business: {},
      foodArr: [],
      user: {},
      showPriceDialog: false,  // 控制弹窗显示
      newPrice: null,  // 存储新输入的价格
      selectedFood: null  // 选中的食品
    };
  },
  created() {
    this.user = this.$getSessionStorage("user");

    // 查询商家信息
    this.$axios
        .post("BusinessController/getBusinessById", this.$qs.stringify({ businessId: this.businessId }))
        .then((response) => {
          this.business = response.data;
        })
        .catch((error) => {
          console.error(error);
        });

    // 查询所属菜品信息
    this.$axios
        .post("FoodController/listFoodByBusinessId", this.$qs.stringify({ businessId: this.businessId }))
        .then((response) => {
          this.foodArr = response.data;
        })
        .catch((error) => {
          console.error(error);
        });
  },
  methods: {
    // 删除菜品
    deleteFood(foodId) {
      if (confirm("确认删除该菜品吗？")) {
        // 发送删除请求时确保传递当前用户的 userId
        this.$axios
            .post("FoodController/removeFood", this.$qs.stringify({ foodId, userId: this.user.userId }))
            .then((response) => {
              if (response.data === 1) {
                this.foodArr = this.foodArr.filter((item) => item.foodId !== foodId);
              } else {
                alert("删除菜品失败！");
              }
            })
            .catch((error) => {
              console.error(error);
            });
      }
    },
    // 打开编辑价格弹窗
    openEditPriceDialog(item) {
      this.selectedFood = item;  // 选中当前商品
      this.newPrice = item.foodPrice;  // 默认显示当前价格
      this.showPriceDialog = true;  // 打开弹窗
    },
    // 关闭弹窗
    closeEditPriceDialog() {
      this.showPriceDialog = false;
      this.selectedFood = null;
    },
    // 更新商品价格
    updateFoodPrice() {
      if (this.newPrice == null || this.newPrice <= 0) {
        alert("请输入有效的价格！");
        return;
      }

      this.$axios.post("FoodController/updateFoodPrice", {
        foodId: this.selectedFood.foodId,
        foodPrice: this.newPrice
      }, {
        headers: {
          'Content-Type': 'application/json'  // 设置请求头为 application/json
        }
      })
          .then(response => {
            console.log("Response Data:", response.data);  // 打印返回的数据
            if (response.data === "商品价格更新成功") {
              // 更新前端数据
              this.selectedFood.foodPrice = this.newPrice;
              this.closeEditPriceDialog();  // 关闭弹窗
            } else {
              alert("更新价格失败！");
            }
          })
          .catch(error => {
            console.error("更新价格失败:", error);
          });
    },
    // 跳转到新增菜品页面
    addNewFood() {
      this.$router.push({
        path: "/addFood",
        query: { businessId: this.business.businessId }  // 确保 businessId 被传递
      });
    }
  }
};
</script>

<style scoped>
/* 商家管理页面样式 */
.wrapper {
  width: 100%;
  height: 100%;
}

header {
  width: 100%;
  height: 12vw;
  background-color: #0097FF;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 4.8vw;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
}

.business-logo {
  margin-top: 12vw;
  display: flex;
  justify-content: center;
  align-items: center;
}

.business-logo img {
  width: 40vw;
  height: 30vw;
  border-radius: 5px;
}

.business-info {
  text-align: center;
  margin-top: 10px;
}

.business-info h1 {
  font-size: 5vw;
}

.business-info p {
  font-size: 3vw;
  color: #666;
}

.food {
  width: 100%;
  margin-top: 20px;
}

.food li {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  border-bottom: 1px solid #ddd;
  align-items: center;
}

.food-left {
  display: flex;
  align-items: center;
}

.food-left img {
  width: 80px;
  height: 80px;
  border-radius: 5px;
  margin-right: 10px;
}

.food-left-info h3 {
  font-size: 18px;
  margin: 0;
}

.food-left-info p {
  color: #888;
  font-size: 14px;
  margin: 2px 0;
}

.food-right {
  font-size: 24px;
  cursor: pointer;
}

.add-food {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  margin-top: 20px;
  cursor: pointer;
}

/* 弹窗样式 */
.price-dialog {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.5);
}

.dialog-content {
  background-color: white;
  padding: 20px;
  border-radius: 5px;
  width: 300px;
  text-align: center;
}

.dialog-content input {
  width: 100%;
  padding: 10px;
  margin-bottom: 10px;
  font-size: 16px;
}

.dialog-actions {
  display: flex;
  justify-content: space-between;
}

.dialog-actions button {
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
}

.price-input {
  width: 100%; /* 使用100%来确保输入框不会溢出弹窗的宽度 */
  max-width: 300px; /* 可选：为输入框设置最大宽度 */
  padding: 8px;
  font-size: 16px;
  border: 1px solid #ccc;
  border-radius: 4px;
  display: block;
  margin: 10px auto;
  box-sizing: border-box; /* 确保内边距不会影响宽度 */
}


</style>
